package com.ruoyi.luban.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.luban.domain.vo.PSchoolVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.luban.mapper.PSchoolMapper;
import com.ruoyi.luban.domain.PSchool;
import com.ruoyi.luban.service.IPSchoolService;

/**
 * 学校Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-28
 */
@Service
public class PSchoolServiceImpl implements IPSchoolService 
{
    @Autowired
    private PSchoolMapper pSchoolMapper;
    @Value("${luban.coordinate.range}")//坐标范围
    private double range;

    /**
     * 查询学校
     * 
     * @param id 学校主键
     * @return 学校
     */
    @Override
    public PSchool selectPSchoolById(Long id)
    {
        return pSchoolMapper.selectPSchoolById(id);
    }

    /**
     * 查询学校列表
     * 
     * @param pSchool 学校
     * @return 学校
     */
    @Override
    public List<PSchool> selectPSchoolList(PSchool pSchool)
    {
        return pSchoolMapper.selectPSchoolList(pSchool);
    }

    /**
     * 新增学校
     * 
     * @param pSchool 学校
     * @return 结果
     */
    @Override
    public int insertPSchool(PSchool pSchool)
    {
        pSchool.setCreateTime(DateUtils.getNowDate());
        return pSchoolMapper.insertPSchool(pSchool);
    }

    /**
     * 修改学校
     * 
     * @param pSchool 学校
     * @return 结果
     */
    @Override
    public int updatePSchool(PSchool pSchool)
    {
        pSchool.setUpdateTime(DateUtils.getNowDate());
        return pSchoolMapper.updatePSchool(pSchool);
    }

    /**
     * 批量删除学校
     * 
     * @param ids 需要删除的学校主键
     * @return 结果
     */
    @Override
    public int deletePSchoolByIds(Long[] ids)
    {
        return pSchoolMapper.deletePSchoolByIds(ids);
    }

    /**
     * 删除学校信息
     * 
     * @param id 学校主键
     * @return 结果
     */
    @Override
    public int deletePSchoolById(Long id)
    {
        return pSchoolMapper.deletePSchoolById(id);
    }

    @Override
    public List<PSchool> getNearbySchools(PSchool pSchool) {
        if(pSchool.getId() != null){
            return Arrays.asList(pSchoolMapper.selectPSchoolById(pSchool.getId()));
        }
        //先计算查询点的经纬度范围
        Map<String,Double> result = new HashMap<>(4);

        double latitude = Double.parseDouble(pSchool.getLatitude());
        double longitude = Double.parseDouble(pSchool.getLongitude());
        double r = 6371;//地球半径千米
        double dlng =  2*Math.asin(Math.sin(range/(2*r))/Math.cos(latitude*Math.PI/180));
        dlng = dlng*180/Math.PI;//角度转为弧度
        double dlat = range/r;
        dlat = dlat*180/Math.PI;
        double minLatitude = latitude-dlat;
        double maxLatitude = latitude+dlat;
        double minLongitude = longitude -dlng;
        double maxLongitude = longitude + dlng;
        return pSchoolMapper.selectSchoolByCoordinateRange(minLatitude,maxLatitude,minLongitude,maxLongitude,pSchool.getSchoolName());
    }
    @Override
    public List<PSchoolVo> selectPSchoolListBySign(Long id) {

        return pSchoolMapper.selectPSchoolListBySign(id);
    }
    @Override
    public int selectPSchoolListBySignCount(Long id) {
        return pSchoolMapper.selectPSchoolListBySignCount(id);
    }
    /**
     * 获取坐标指定的范围
     * @param longitude
     * @param latitude
     * @return
     */
    private Map<String,Double> getCoordinateRange(double longitude, double latitude){
        //先计算查询点的经纬度范围
        Map<String,Double> result = new HashMap<>(4);
        double r = 6371;//地球半径千米
        double dlng =  2*Math.asin(Math.sin(range/(2*r))/Math.cos(latitude*Math.PI/180));
        dlng = dlng*180/Math.PI;//角度转为弧度
        double dlat = range/r;
        dlat = dlat*180/Math.PI;
        double minLatitude =latitude-dlat;
        double maxLatitude = latitude+dlat;
        double minLongitude = longitude -dlng;
        double maxLongitude = longitude + dlng;
        result.put("minLatitude",minLatitude);
        result.put("maxLatitude",maxLatitude);
        result.put("minLongitude",minLongitude);
        result.put("maxLongitude",maxLongitude);
        return result;
    }
}
